﻿
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;

using GECS.Core;

namespace GECS.GraphicsUtil {

	/// <summary>
	/// This class is used to load the fonts from their respective font-descriptor
    /// files. They are created like this.
    /// <code>
    /// GFont font = GFontAdvanced.GetFont(Game.LoadImage(&quot;&lt;path-to-image&gt;&quot;), &quot;&lt;path-to-descriptor&gt;&quot;);
    /// </code>
	/// </summary>
    public class GFontAdvanced {
		
		/// <summary>
		/// Creates a new GFont object by parsing the data from a font-descriptor.
		/// Just type the characters as in the same order of the image file. Note
		/// that all the characters should be in the same width and size.
		/// </summary>
		/// <param name="img">The image file containing the characters.</param>
		/// <param name="font_descriptor">The font-descriptor path.</param>
		/// <returns>The created GFont object.</returns>
		public static GFont GetFont(Image img, string font_descriptor){
			List<string> lines = new List<string>();
			using (Stream stream = Game.GetLibraryResourceStream(font_descriptor)){
				using (StreamReader reader = new StreamReader(stream)){
					string line;
					while ((line = reader.ReadLine()) != null){
						lines.Add(line);
					}
				}
			}
			int rows = 0;
			int cols = 0;
			string data = "";
			for (int i=0; i<lines.Count; i++){
				rows++;
				cols = Math.Max(cols, lines[i].Length);
				data += lines[i];
			}
			return new GFont(img, rows, cols, data);
		}
		
		/// <summary>
		/// Creates a font by using the default font descriptor.
		/// </summary>
		/// <param name="img">The image file containing the characters.</param>
		/// <returns>The created GFont object.</returns>
		public static GFont GetFont(Image img){
			return GetFont(img, "ImageFonts/DefFontDescriptor.txt");
		}
		
    }
	
}
